import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_study/config/ConstColor.dart';
import 'package:flutter_study/config/ConstString.dart';
import 'package:flutter_study/model/topic_detail_model.dart';
import 'package:flutter_study/page/video_detail_page.dart';
import 'package:flutter_study/util/date_util.dart';
import 'package:flutter_study/util/navigator_manager.dart';
import 'package:flutter_study/util/share_util.dart';

class TopicDetailWidgetItem extends StatelessWidget {
  final TopicDetailItemData model;

  const TopicDetailWidgetItem({Key key, this.model}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Column(
        children: [
          _headWidget(),
          _desWidget(),
          _tagWidget(),
          _feedWidget(context),
          _consumptionWidget(),
          _dividerWidget()
        ],
      ),
      onTap: () {
        NavigatorManager.to(VideoDetailPage(
          data: model.data.content.data,
        ));
      },
    );
  }

  _headWidget() {
    return Row(
      children: [
        Padding(
          padding: EdgeInsets.fromLTRB(20, 20, 10, 0),
          child: ClipOval(
            child: CachedNetworkImage(
              width: 45,
              height: 45,
              imageUrl:
                  model.data.header.icon == null ? "" : model.data.header.icon,
            ),
          ),
        ),
        Expanded(
            child: Padding(
          padding: EdgeInsets.fromLTRB(0, 20, 20, 0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                model.data.header.issuerName == null
                    ? ''
                    : model.data.header.issuerName,
                style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 14,
                    color: Colors.black),
              ),
              Row(
                children: [
                  Text(
                    "${DateUtils.formatDateMsByYMD(model.data.header.time)}发布：",
                    style:
                        TextStyle(color: ConstColor.hitTextColor, fontSize: 12),
                  ),
                  Expanded(
                      child: Text(
                    model.data.content.data.title,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                        color: Colors.black,
                        fontSize: 12,
                        fontWeight: FontWeight.bold),
                  ))
                ],
              )
            ],
          ),
        ))
      ],
    );
  }

  _desWidget() {
    return Padding(
        padding: EdgeInsets.fromLTRB(20, 0, 20, 10),
        child: Text(
          model.data.content.data.description,
          style: TextStyle(fontSize: 14, color: ConstColor.desTextColor),
          overflow: TextOverflow.ellipsis,
          maxLines: 2,
        ));
  }

  _tagWidget() {
    return Padding(
        padding: EdgeInsets.fromLTRB(20, 0, 20, 10),
        child: Row(
          children: _getTagWidgetList(model),
        ));
  }

  _feedWidget(context) {
    return Padding(
      padding: EdgeInsets.fromLTRB(20, 0, 20, 5),
      child: Stack(
        children: [
          ClipRRect(
            child: Hero(
              tag: "${model.id}${model.data.content.data.time}",
              child: CachedNetworkImage(
                width: MediaQuery.of(context).size.width,
                height: 200,
                imageUrl: model.data.content.data.cover.feed,
                fit: BoxFit.cover,
                errorWidget: (context, url, error) {
                  return Image.asset("images/img_load_fail.png");
                },
              ),
            ),
          ),
          Positioned(
              right: 8,
              bottom: 8,
              child: ClipRRect(
                borderRadius: BorderRadius.circular(5),
                child: Container(
                  decoration: BoxDecoration(color: Colors.black54),
                  padding: EdgeInsets.all(5),
                  child: Text(
                    DateUtils.formatDateMsByMs(
                        model.data.content.data.duration * 1000),
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 12,
                        fontWeight: FontWeight.bold),
                  ),
                ),
              ))
        ],
      ),
    );
  }

  _consumptionWidget() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: [
        Row(
          children: [
            Icon(
              Icons.favorite_border,
              size: 20,
              color: ConstColor.hitTextColor,
            ),
            Padding(
              padding: EdgeInsets.only(left: 10),
              child: Text(
                "${model.data.content.data.consumption.collectionCount}",
                style: TextStyle(fontSize: 12, color: ConstColor.hitTextColor),
              ),
            )
          ],
        ),
        Row(
          children: [
            Icon(
              Icons.message,
              size: 20,
              color: ConstColor.hitTextColor,
            ),
            Padding(
              padding: EdgeInsets.only(left: 10),
              child: Text(
                "${model.data.content.data.consumption.replyCount}",
                style: TextStyle(fontSize: 12, color: ConstColor.hitTextColor),
              ),
            ),

          ],
        ),
        Row(
          children: [
            Icon(
              Icons.star_border,
              size: 20,
              color: ConstColor.hitTextColor,
            ),
            Padding(
              padding: EdgeInsets.only(left: 10),
              child: Text(
                ConstString.collect_text,
                style: TextStyle(fontSize: 12, color: ConstColor.hitTextColor),
              ),
            ),

          ],
        ),
        IconButton(
            icon: Icon(
              Icons.share,
              color: ConstColor.hitTextColor,
            ),
            onPressed: () {
              ShareUtil.share(model.data.content.data.title,
                  model.data.content.data.webUrl.forWeibo);
            })
      ],
    );
  }

  _dividerWidget() {
    return Padding(
      padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
      child: Divider(
        height: 0.5,
      ),
    );
  }

  _getTagWidgetList(TopicDetailItemData model) {
    var list = model.data.content.data.tags.map((e) {
      return Container(
        margin: EdgeInsets.only(right: 5),
        child: Text(
          e.name,
          style: TextStyle(fontSize: 12, color: Colors.blue),
        ),
        alignment: Alignment.center,
        height: 20,
        padding: EdgeInsets.only(left: 5, right: 5),
        decoration: BoxDecoration(
            color: ConstColor.tabBgColor,
            borderRadius: BorderRadius.circular(4)),
      );
    }).toList();
    return list.length > 3 ? list.sublist(0, 3) : list;
  }
}
